#!/bin/perl

# process Ottawa text file into format I can paste into Excel:
# team name <tab> time


my $fileName;
if ($#ARGV >= 0)
{
   $fileName = $ARGV[0]; 
}
else
{
   $fileName = "D:\\dragon boat\\2009\\Regression\\OttawaSat.txt";
}


my $outFile;
my $i = 0;

my $teamName = 0;
my $category = 0;
my $time = 0;
my $raceNo = 0;
my $distance = 500;
my $festival = "Ottawa";
my $year = 2009;

print("Starting parser\n");

# Read file into memory
# -----------------------------------------------------------------------------
open(F, "<$fileName") || die("Could not open $fileName");
my @lines=<F>;
my $line;
close(F);


$outFile = "$fileName.out";
open(F, ">$outFile") || die("Could not open $outFile");

$year = 2009;
$festival="Nationals";
$distance = 500;
$category = "Mixed";
for ($i=1;$i<=$#lines; $i++)
{
   $line=$lines[$i];
   chomp($line);


   if ($line =~ m/(\d+)\s+Teams\s+Rank\s+Time/ )
   {
      $raceNo = $1;
   }

   if ($line =~ m/^Race/ )
   {
      if ($line =~ m/Mixed/)
      {
         $category="Mixed";
      }
      elsif ($line =~ m/Women/)
      {
         $category="Women";
      }
      elsif ($line =~ m/Open/)
      {
         $category="Open";
      }
      else
      {
         # it didn't seem to specify.  Assume mixed
         $category="Mixed";
      }
      
      
      if ($line =~ m/250m/)
      {
         $distance=250;
      }
      elsif ($line =~ m/500m/)
      {
         $distance=500;
      }
      elsif ($line =~ m/2000m/)
      {
         $distance=2000;
      }
      else
      {
         # it didn't seem to specify.  Assume 500
         $distance=500;
      }
      
   }


   if ($line =~ m/(\d+)\s+(.*)\s+\d\s+(\d+:\d\d\.\d\d)/ )
   {
      #print("1=$1          2=$2 \n");
      $lane = $1;
      $teamName = $2;
      $time = $3;

      

     #$teamName =~ s/Provincial & Federal Government//;


      

      print F "$teamName,$category,$time,$raceNo,$distance,$festival,$year,$lane\n";

   }      
}
   
close(F);



